<?php


namespace app\common\model\supplier;

use app\common\model\BaseModel;
use app\common\model\product\Brand;
use app\common\model\proxy\User;

/**
 * 供应商服务保障模型
 */
class ServiceSecurity extends BaseModel
{
    protected $pk = 'service_security_id';
    protected $name = 'supplier_service_security';

    /**
     * 代理
     * @return \think\model\relation\BelongsTo
     * @Author joey
     * @date 2024/3/29 15:22
     */
    public function proxy()
    {
        return $this->belongsTo(User::class, 'proxy_id', 'proxy_id');
    }

    /**
     * 所有
     * @return \think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getALL($map = [])
    {
        $model = new static;
        if ($map) {
            $model = $model->where($map);
        }
        return $model->with(['proxy'])->order(['create_time' => 'asc'])->select();
    }

    /**
     * 详情
     */
    public static function detail($service_security_id)
    {
        return (new static())->find($service_security_id);
    }

    /**
     * 隐藏字段
     */
    public static function listData($where, $params, $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $model = self::assWhere($model, $where);
        $list  = $model->paginate($params);
        return $list;
    }

    public static function createData($where)
    {
        $where           = self::disposeWhere($where);
        $model           = new static();
        $where['app_id'] = self::$app_id;
        $data            = $model->save($where);
        return $data;
    }

    public static function one($where)
    {
        $where = self::disposeWhere($where);
        $model = new static();
        return $model->findOrFail($where);
    }

    public static function deleteData($where)
    {
        $model = new static();
        return $model->where(['id' => $where['id']])->delete();
    }

    public static function listAll($where = [], $with = [], $sort = '')
    {
        $where = self::disposeWhere($where);
        $model = new static();
        if (count($with) > 0) {
            $model = $model->with($with);
        }
        $model = self::assWhere($model, $where);
        if (!empty($sort)) {
            $model = $model->order($sort);
        } else {
            $model = $model->order(self::baseOrder());
        }
        $list = $model->select();
        return $list;
    }
}
